/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.grnt.buddy;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.grnt.buddy.BuddiesTreeControler.BuddiesCtlException;
import org.grnt.common.CommonFunc;
import org.grnt.dbcommon.ControlBean;
import static org.grnt.dbcommon.DataControler.TransOpt;

/**
 * It is subclass of ControlBean to implement delete a buddy action.
 * @author sayid
 */
public class DelBuddy implements ControlBean {

	private final Long buddyId;
	private final String sqlStr = "DELETE FROM gt_buddies_tree WHERE buddy_id = ?";
	DelBuddy(Long buddyId) {
		this.buddyId = buddyId;
	}

	public List<String> getCommands() {
		String command = CommonFunc.sqlStrFormat(sqlStr, buddyId.toString());
		CommonFunc.sqlNullStr2NullVal(command);
		List<String> result = new ArrayList<String>();
		result.add(command);
		return result;
	}

	public boolean doInTransaction() {
		try {
			BuddiesTreeControler.delBuddy(buddyId);
			return true;
		} catch (BuddiesCtlException ex) {
			Logger.getLogger(DelBuddy.class.getName()).log(Level.SEVERE, null, ex);
			ex.printStackTrace();
			return false;
		}
	}

	public void doAfterTransaction(TransOpt opt) {
		return;
	}

}